package com.xhwl.centiotdata.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.enums.device.BaAttrsEnum;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C),1998-2022, 兴海物联
 * FileName: BaAttrsUtil
 * Author: XuChang
 * Date: 2022/3/2 14:25
 * Description: 奥莱ba设备属性解析
 * History:
 */
@Slf4j
public class BaAttrsUtil {

    Map<BaAttrsEnum, Boolean> map;

    public BaAttrsUtil(Map<BaAttrsEnum, Boolean> map) {
        this.map = map;
    }

    /**
     * 如果拥有相关枚举，并且相关枚举的值为flag，就返回true，否则false
     *
     * @param baAttrsEnum
     * @param flag
     * @return
     */
    public boolean checkAttr(boolean flag, BaAttrsEnum baAttrsEnum) {
        if (map == null || map.size() == 0 || baAttrsEnum == null)
            return false;
        if (map.containsKey(baAttrsEnum) && map.get(baAttrsEnum) == flag)
            return true;
        return false;
    }

    public boolean checkAttr(boolean flag, BaAttrsEnum... baAttrsEnums) {
        for (BaAttrsEnum baAttrsEnum : baAttrsEnums) {
            if (checkAttr(flag, baAttrsEnum))
                return true;
        }
        return false;
    }

    /**
     * 从状态数据json获取需要的属性值,返回示例请参考 com.xhwl.centiotdata.UtilTest#testJson()
     *
     * @param json
     * @return
     */
    public static Map<BaAttrsEnum, Boolean> getBaAttrs(String json) {
        if (StringUtils.isEmpty(json)) {
            log.info("getBaAttrs()获取到的json结果为空");
            return null;
        }
        Map<BaAttrsEnum, Boolean> result = new HashMap<>();
        try {
            Map<String, Object> map = JSON.parseObject(json);
            Object data = map.get("data");
            if (data == null) {
                log.info("data==null,json=" + json);
                return null;
            }
            JSONObject jsonObject = JSONObject.parseObject(data.toString());
            Object attrs = jsonObject.get("attrs");
            JSONArray objects = JSONArray.parseArray(attrs.toString());
            for (Object object : objects) {
                JSONObject obj = JSON.parseObject(object.toString());
                Object name = obj.get("name");
                if (name != null) {
                    BaAttrsEnum anEnum = BaAttrsEnum.getEnum(name.toString());
                    if (anEnum != null) {
                        Object value = obj.get("value");
                        JSONObject jsonObj = getJsonObj(value);
                        //如果不包含key，默认认为状态false
                        if (!jsonObj.containsKey("Value")) {
                            result.put(anEnum, false);
                            continue;
                        }
                        Object value1 = jsonObj.get("Value");
                        Boolean status = null;
                        if (value1 instanceof String) {
                            //部分ba返回的状态是字符串，部分是布尔值
                            if ("True".equals(value1) || "true".equals(value1))
                                status = true;
                            else
                                status = false;
                        } else if (value1 instanceof Boolean) {
                            status = (Boolean) value1;
                        }
                        if (status == null)
                            continue;
                        else
                            result.put(anEnum, status);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    private static JSONObject getJsonObj(Object o) {
        if (o == null) return null;
        JSONObject obj = JSON.parseObject(o.toString());
        return obj;
    }
}
